package com.controller.buying 
{

	import com.events.buying.GoodsReceivedEvent;
	import com.model.valueObjects.VoGoodsReceived;
	import com.service.GoodsReceivedMaster;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	public class GoodsReceivedController extends AbstractController
	{		
		[Inject]
		public var goodsReceivedService:GoodsReceivedMaster;
		
		public function GoodsReceivedController()
		{
			super();
		}
		
		[EventHandler(event="GoodsReceivedEvent.GET_ALL")]
		public function getAll():void{
			sh.executeServiceCall(goodsReceivedService.getAllGR(),getAllResult, handleFault );
		}
		private function getAllResult(event:ResultEvent):void
		{
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
			
			if(model.currentMenu=='grBtn')
				model.currentBuyTableIndex=3;
			
			model.poStatus='view';
			model.goodsReceivedList=event.result as ArrayCollection;
		}
		
		[EventHandler(event="GoodsReceivedEvent.ADD", properties="goodsReceived_vo")]
		public function add(goodsReceived_vo:VoGoodsReceived):void
		{
			sh.executeServiceCall(goodsReceivedService.addNewGR(goodsReceived_vo),addResult, handleFault );	
		}
		private function addResult(event:ResultEvent):void
		{
			model.grCode=event.result as String;
			if(model.saveCloseBtn)model.currentBuyFormsIndex=0;	
			Alert.show( "Goods Received  added" );	
			
			getAll();
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="GoodsReceivedEvent.UPDATE", properties="goodsReceived_vo")]
		public function update(goodsReceived_vo:VoGoodsReceived):void
		{
			sh.executeServiceCall(goodsReceivedService.editGR(goodsReceived_vo), editResult, handleFault );	
		}
		private function editResult(event:ResultEvent):void
		{
			model.grCode=event.result as String;
			getAll();
			if(model.saveCloseBtn)model.currentBuyFormsIndex=0;	
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;			
			Alert.show( "Goods Received  Updated" );	
		}
		
		[EventHandler(event="GoodsReceivedEvent.DELETE", properties="goodsReceived_vo")]
		public function deletef(goodsReceived_vo:VoGoodsReceived):void
		{
			sh.executeServiceCall(goodsReceivedService.deleteGR(goodsReceived_vo),deleteResult, handleFault );	
		}
		private function deleteResult(event:ResultEvent):void
		{
			model.currentBuyFormsIndex=0;
			getAll();
			Alert.show( "Goods Received Deleted" );	
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="GoodsReceivedEvent.GET_GR_LIST",properties="supplierFk")]
		public function getGRList(supplierFk:Number):void{
			sh.executeServiceCall(goodsReceivedService.getGRForCRDB(supplierFk),getGRListResult, handleFault );
		}
		private function getGRListResult(event:ResultEvent):void
		{
			model.grHeaderList=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		[EventHandler(event="GoodsReceivedEvent.GET_PO_LIST",properties="supplierFk")]
		public function getPOList(supplierFk:Number):void{
			sh.executeServiceCall(goodsReceivedService.getPOListforGR(supplierFk),getPOListResult, handleFault );
		}
		private function getPOListResult(event:ResultEvent):void
		{
			model.grPOList=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		private function handleFault(event:FaultEvent):void
		{
			Alert.show( event.fault.faultDetail, "Error" );
		}
	}
}